package com.itheima.service.company.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.utils.UtilFuns;
import com.itheima.dao.system.CompanyDao;
import com.itheima.doamin.system.Company;
import com.itheima.service.company.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    CompanyDao companyDao;
    /**
     * 查询所有
     */
    @Override
    public List<Company> findAll() {
        return companyDao.findAll();
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @Override
    public Company findById(String id) {
        return companyDao.findById(id);
    }

    /**
     * 保存操作
     *
     * @param company
     */
    @Override
    public void save(Company company) {
        company.setId(UtilFuns.getUUID());
        company.setExpirationDate(new Date());
        companyDao.save(company);
    }

    /**
     * 修改操作
     *
     * @param company
     */
    @Override
    public void update(Company company) {
        company.setExpirationDate(new Date());
        companyDao.update(company);
    }

    /**
     * 删除操作
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        companyDao.delete(id);
    }

    /**
     * 分页
     * @param page
     * @param size
     * @return
     */
    public PageResult findPage(int page,int size){
        //总条数
        //每页查询结果集
        PageResult pageResult=new PageResult
                (companyDao.finTotal(),companyDao.findPageList((page-1)*size,size),page,size);
        return pageResult;
    }

    /**
     * helper插件分页
     * @param page
     * @param size
     * @return
     */
    public PageInfo<Company> findByPageHelper(int page,int size){
        //总条数
        //每页查询结果集
        PageHelper.startPage(page, size);
        List<Company> companies = companyDao.findAll();
        PageInfo<Company> pageInfo=new PageInfo<>(companies);
        return pageInfo;
    }
}
